package com.zbcz.erp.activity

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.android.material.tabs.TabLayoutMediator
import com.zbcz.erp.databinding.ActivityHomeBinding
import com.zbcz.erp.fragment.CommonListFragment
import com.zbcz.erp.fragment.CommonListFragment.Companion.MY_APPLY
import com.zbcz.erp.fragment.CommonListFragment.Companion.MY_DONE_LIST
import com.zbcz.erp.fragment.CommonListFragment.Companion.MY_TO_DO_LIST
import com.zbcz.erp.util.DensityUtils
import com.zbcz.erp.util.MAIN_ROUTE
import com.zbcz.erp.util.onSingleClick
import com.zbcz.erp.viewmodel.MainActivityViewModel


@Route(path = MAIN_ROUTE)
class MainActivity : BaseActivity() {

    private lateinit var mBinding: ActivityHomeBinding

    private val mViewModel: MainActivityViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        mBinding = ActivityHomeBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        super.onCreate(savedInstanceState)
        init()
    }

    private fun init() = with(mBinding) {
        val adapter = HomePageFragmentAdapter(supportFragmentManager, lifecycle).apply {
            addFragment(CommonListFragment.createInstance("我的待办", MY_TO_DO_LIST))
            addFragment(CommonListFragment.createInstance("我的已办", MY_DONE_LIST))
            addFragment(CommonListFragment.createInstance("我的申请", MY_APPLY))
        }
        viewPager2.adapter = adapter
        TabLayoutMediator(tabLayout, viewPager2) { tab, position ->
//            tab.text = "Tab ${position + 1}" // 设置选项卡标题，可以根据实际情况修改
            when (position) {
                0 -> {
                    tab.text = "我的待办"
                }

                1 -> {
                    tab.text = "我的已办"
                }

                2 -> {
                    tab.text = "我的申请"
                }
            }
        }.attach()
//        clParent.setPadding(
//            0,
//            DensityUtils.dp2px(this@MainActivity, getStatusBarHeight(this@MainActivity).toFloat()),
//            0,
//            0
//        )

        clParent.setPadding(
            0,
            DensityUtils.dp2px(this@MainActivity, 48f),
            0,
            0
        )
        // TEST ONLY
        mBinding.ivAddIcon.onSingleClick {
            //startActivity(Intent(this@MainActivity, MyApplyActivity::class.java))
//            startActivity(
//                Intent(
//                    this@MainActivity,
//                    ErpWorkProcessActivity::class.java
//                )
//            )
            startActivity(
                Intent(
                    this@MainActivity,
                    MyApplyActivity::class.java
                )
            )
//            startActivity(
//                Intent(
//                    this@MainActivity,
//                    ChooseBusinessTripActivity::class.java
//                )
//            )
        }

    }

    class HomePageFragmentAdapter(
        fragmentManager: FragmentManager,
        lifecycle: Lifecycle
    ) : FragmentStateAdapter(fragmentManager, lifecycle) {

        private val fragmentList = mutableListOf<Fragment>()

        fun addFragment(fragment: Fragment) {
            fragmentList.add(fragment)
        }

        override fun getItemCount(): Int {
            return fragmentList.size
        }

        override fun createFragment(position: Int): Fragment {
            return fragmentList[position]
        }
    }

    // 标记用户是否已按下过后退键
    private var exitTime: Long = 0

    override fun onBackPressed() {
        // 获取当前时间
        val currentTime = System.currentTimeMillis()

        // 如果两次点击间隔小于2秒，则退出应用
        if ((currentTime - exitTime) > 2000) {
            // 第一次点击后退键，显示Toast并设置退出时间
            Toast.makeText(this, "再按一次退出应用", Toast.LENGTH_SHORT).show()
            exitTime = currentTime
        } else {
            // 第二次点击后退键，退出应用
            super.onBackPressed()
            System.exit(0) // 注意：这不是推荐的做法，只是为了演示
            // 推荐使用Activity的finish()方法退出，如果需要完全退出应用，请考虑使用其他方式，如发送广播等
        }
    }
}